import MainController from "./MainController";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Barrier extends cc.Component {

    @property(cc.Label)
    lbScore: cc.Label = null;

    @property(Boolean)
    isAddBuffBall: boolean = false;

    score: number = 2;

    main: MainController;

    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        this.setScore(this.score);
    }

    start() {
        if (!this.isAddBuffBall) {
            this.lbScore.node.rotation = -this.node.rotation;
        }
    }

    // update (dt) {}

    setScore(score: number) {
        if (score > 0) {
            if (this.lbScore) {
                this.score = score;
                this.lbScore.string = this.score.toString()
            }
        } else if (score === 0) {
            this.main.removeBarrier(this);
        }

    }

    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
        if (this.isAddBuffBall) {
            this.main.addBall(this.node.position);
            this.main.removeBarrier(this);
            //把自己删除，同时产生新的球
        } else {
            this.setScore(this.score - 1);
        }
    }

}
